package com.xiaoxu.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.AutoInfo;
import com.xiaoxu.rental.entity.Maintain;
import com.xiaoxu.rental.service.AutoInfoService;
import com.xiaoxu.rental.service.MaintainService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "车辆保养")
@RestController
@RequestMapping("/rental/maintain")
@SuppressWarnings("all")
public class MaintainController {


    @Resource
    private AutoInfoService autoInfoService;
    @Resource
    private MaintainService maintainService;
    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody Maintain maintain){
        Page<Maintain> page = new Page<>(start, size);
        return Result.success(maintainService.searchByPage(page, maintain));
    }
    @PostMapping
    public Result save(@RequestBody Maintain maintain) {
        //设置保养关联的车辆信息
        AutoInfo autoInfo =
                autoInfoService.selectAutoNum(maintain.getAutoNum());
        maintain.setAutoId(autoInfo.getId());
        //将车辆实保次数+1
        autoInfo.setActualNum(autoInfo.getActualNum() + 1);
        autoInfoService.updateById(autoInfo);
        return maintainService.save(maintain)?
                Result.success():Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody Maintain maintain) {
        return maintainService.updateById(maintain)?
                Result.success():Result.fail();
    }
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable String ids) {
        return maintainService.delete(ids)?Result.success():Result.fail();
    }

}
